package gameobject;

import java.awt.event.KeyEvent;
import simulation.Game;

/**
 * Player class handles the key events
 * for arrow keys and space bar to
 * move and shoot missiles respectively.
 *
 */
/**
 * @author Volodymyr, Rex
 *
 */
public class Player extends Sprite {

    //private String type = "player";
    
    /**
     * Empty constructor.
     */
    public Player() {
        super();
    }

    /**
     * Constructor for player.
     * @param game which the player belongs to
     * @param dx starting x acc
     * @param dy starting y acc
     * @param speed starting speed
     * @param hp starting health
     * @param damage starting damage to inflict
     */
    public Player (Game game, Number dx, Number dy,
            Number speed, Number hp, Number damage) {
        super(game, dx, dy, speed, hp, damage);
    }

    @Override
    public Player createProduct(Game game, String[] args) {
        Number[] parameters = convertToParameters(args);
        Player myProduct = new Player(game, parameters[findIndex("dx")],
                parameters[findIndex("dy")], parameters[findIndex("speed")],
                parameters[findIndex("HP")], parameters[findIndex("damage")]);
        myProduct.setImage(args[findIndex("image")]);
        return myProduct;
   }
    
   /* public void setType(String s){
        type = s;
    }*/
    
    @Override
    public void collide(Sprite other) {
        if (!(other instanceof Missile)) {
            die();
        }
    }

    @Override
    public void update() {
        super.update();
    }

    @Override
    protected void checkBounds() {
        if (getX() < 0) {
            setX(0);
        }
        else if (getX() > getDimension().width - getWidth()) {
            setX(getDimension().width - getWidth());
        }
        if (getY() > getDimension().height - getHeight()) {
            setY(getDimension().height - getHeight());
        }
        else if (getY() < 0) {
            setY(0);
        }
    }

    @Override
    public void handleKeyPressed(KeyEvent e) {
        int lastKeyPressed = e.getKeyCode();
        if (lastKeyPressed == KeyEvent.VK_UP) {
            //setDy(getDy() - speedIncrement);
            // if (!type.equalsIgnoreCase("craft"))
                setDy(-1);
        }
        if (lastKeyPressed == KeyEvent.VK_DOWN) {
            //setDy(getDy() + speedIncrement);
           // if (!type.equalsIgnoreCase("craft"))
                setDy(1);
        }
        if (lastKeyPressed == KeyEvent.VK_LEFT) {
            //setDx(getDx() - speedIncrement);
            setDx(-1);
        }
        if (lastKeyPressed == KeyEvent.VK_RIGHT) {
            //setDx(getDx() + speedIncrement);
            setDx(1);
        }
    }

    @Override
    public void handleKeyReleased(KeyEvent e) {
        int lastKeyReleased = e.getKeyCode();
        if ((lastKeyReleased == KeyEvent.VK_UP)
                || (lastKeyReleased == KeyEvent.VK_DOWN)) {
            setDy(0);
        }
        if ((lastKeyReleased == KeyEvent.VK_LEFT)
                || (lastKeyReleased == KeyEvent.VK_RIGHT)) {
            setDx(0);
        }
        if (lastKeyReleased == KeyEvent.VK_SPACE) {
            Missile missile = new Missile(getGame(),
                    this, getX(), getY(), 0, -1, Integer.parseInt(getResource("MissileSpeed")), 1, 1);
            addOwnSprite(missile);
        }
    }
}
